package com.dxb.idmr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxb.idmr.entity.Students;
import com.dxb.idmr.entity.StudentsPapers;
import com.dxb.idmr.service.IStudentsPapersService;
import com.dxb.idmr.service.IStudentsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author dxb
 * @since 2020-05-01
 */
@RestController
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    IStudentsService studentsService;
    @Autowired
    IStudentsPapersService studentsPapersService;
    @Autowired
    HttpServletRequest request;

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "增")
    public String add(@RequestBody Students student) throws Exception {
        if (studentsPapersService.count(new QueryWrapper<StudentsPapers>().eq("SNO", student.getSno())) > 0) {
            return "error?msg=已答过题";
        }
        if (studentsService.count(new QueryWrapper<Students>().eq("SNO", student.getSno())) == 0) {
            studentsService.save(student);
        }
        request.getSession().setAttribute("student", student);
        return "ok";
    }
}
